<template>
  <el-table :data="adminData" style="width: 100%">
    <el-table-column fixed prop="id" label="管理员id" width="290" />
    <el-table-column prop="username" label="管理员账号" width="480" />
    <el-table-column prop="password" label="管理员密码" width="480" />
    <el-table-column fixed="right" label="操作" width="250">
      <template #default="scoped">
        <el-button type="danger" round @click="deleteAdmin(scoped.row.id)"
          >注销管理员信息</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页搜索 -->
  <el-pagination
    v-model:current-page="currentPage"
    v-model:page-size="pageSize"
    :page-sizes="[3, 5, 10, 15]"
    layout="jumper, total, sizes, prev, pager, next"
    background
    :total="pageTotal"
    @size-change="pageChange"
    @current-change="currentChange"
    style="margin-top: 20px; justify-content: flex-end"
  />
</template>

<script lang="ts" setup>
import { onMounted, ref, reactive } from "vue";
import { getAllAdminInfo, deleteAdminInfo } from "@/api/admin.js";
import { ElMessageBox } from "element-plus";
import { ElMessage } from "element-plus";

const adminData = ref([]);

const currentPage = ref(1);
const pageSize = ref(3);
const name = ref("");

const deleteAdmin = async (id) => {
  ElMessageBox.confirm("你确认要注销管理员信息吗?", "温馨提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      //调用接口
      let result = await deleteAdminInfo(id);
      ElMessage({
        type: "success",
        message: "删除成功",
      });
      //刷新列表
      getAllInfo();
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "用户取消了删除",
      });
    });
};

//总共页数
const pageTotal = ref(0);

//页数改变时
const pageChange = async (size) => {
  pageSize.value = size;
  getAllInfo();
};

//页数大小改变时
const currentChange = async (num) => {
  currentPage.value = num;
  console.log(10086);
  console.log(num);
  getAllInfo();
};

//获取所有信息
const getAllInfo = async () => {
  let params = {
    page: currentPage.value,
    pageSize: pageSize.value,
  };
  let result = await getAllAdminInfo(params);
  console.log(result.data);
  adminData.value = result.data.records;
  pageTotal.value = result.data.total;
};

onMounted(() => {
  getAllInfo();
});
</script>
